package com.mk.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mk.base.service.impl.BaseService;
import com.mk.mapper.RoleDirDMapper;
import com.mk.model.entity.RoleDirD;
import com.mk.service.IRoleDirDService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 3:25 PM 3/21/18
 */
@Service
public class RoleDirDService extends BaseService<RoleDirDMapper,RoleDirD> implements IRoleDirDService {
    @Override
    public RoleDirD save(Long roleId, Long dDir) {
        if(roleId == null || dDir == null)
            return null;
        RoleDirD roleDirD = new RoleDirD();
        roleDirD.setRoleId(roleId);
        roleDirD.setdDir(dDir);
        return super.insert(roleDirD) ? roleDirD : null;
    }

    @Override
    public boolean deleteByDir(Long dirId) {
        return super.delete(new EntityWrapper<RoleDirD>().eq("d_dir",dirId));
    }

    @Override
    public boolean changeRoleDDir(Long roleId, List<Long> dDirs) {
        if(roleId == null)
            return Boolean.FALSE;
        super.delete(new EntityWrapper<RoleDirD>().eq("role_id",roleId));
        List<RoleDirD> roleDirDS = new ArrayList<>();
        if(!CollectionUtils.isEmpty(dDirs)){
            for (Long dDir : dDirs) {
                RoleDirD roleDirD = new RoleDirD();
                roleDirD.setRoleId(roleId);
                roleDirD.setdDir(dDir);
                roleDirDS.add(roleDirD);
            }
        }
        return super.insertBatch(roleDirDS);
    }

    @Override
    public List<Long> findByRoleId(Long roleId) {
        return baseMapper.findByRoleId(roleId);
    }

    @Override
    public RoleDirD findByDir(Long roleId, Long dDir) {
        return super.selectOne(new EntityWrapper<RoleDirD>().eq("role_id",roleId)
                .eq("d_dir",dDir));
    }
}
